package yxy.game.pm2.command.soul;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.souldata;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Soul;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.hero.SoulHandle;

import java.util.Collection;
import java.util.List;

/**
 * 魂玉 魂玉重铸
 */
@MessageType(proto = Command.SoulRecoin, classtype = HeroMessage.SoulRecoinRequest.class)
public class CMD_SoulRecoin extends Command<HeroMessage.SoulRecoinRequest> {

    @Override
    public void execute(MessageSource messageSource, HeroMessage.SoulRecoinRequest request) {
        String playerCode = messageSource.getTag();
        String soulCode = request.getCode();
        List<Integer> propertyLocksList = request.getPropertyLocksList();//锁上的属性词条
        List<Integer> skillLocksList = request.getSkillLocksList();//锁上的技能词条

        HeroMessage.SoulRecoinResponse.Builder response = HeroMessage.SoulRecoinResponse.newBuilder();

        try {
            Soul soul = Soul.get(playerCode, soulCode);
            if (soul == null) {
                send(messageSource, response, Result.ParamError);
                return;
            }
            souldata data = DataCenter.getData(soul.getDataId(), souldata.class);
            Collection<Currency> cost = Currency.create(data.getRecosttype(), data.getRecostid(), data.getRecostnum());

            int total = 0;
            if (skillLocksList != null && skillLocksList.size() > 0) {
                //如果加上了锁，就添加锁的消耗
                total += skillLocksList.size();
            }

            if (propertyLocksList != null && propertyLocksList.size() > 0) {
                //如果加上了锁，就添加锁的消耗
                total += propertyLocksList.size();
            }

            if (total != 0) {
                int pow = (int) Math.pow(data.getLocknum(), total);
                Currency lockCost = Currency.create(data.getLocktype(), data.getLocktid(), pow);
                cost.add(lockCost);
            }


            try {
                new CurrencyHandle().cost(playerCode, CCReason.魂玉, cost);
            } catch (CurrencyNotEnoughException e) {
                send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
                return;
            }
            soul.recoin(playerCode, propertyLocksList, skillLocksList);

            response.setSoul(new SoulHandle().packSoul(soul));
            if (soul.getRePropertyIds() != null) {
                response.addAllReproperty(soul.getRePropertyIds());
            }
            if (soul.getReSkillIds() != null) {
                response.addAllReskill(soul.getReSkillIds());
            }

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, HeroMessage.SoulRecoinResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}